package net.example.utils.android;

import android.content.Context;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class AndroidUI {

    /**
     * 左右晃动动画
     *
     * @param counts 0.5秒钟晃动多少下
     * @return
     */
    public static void shakeAnimationHorizontal(int counts, View view) {
        Animation animation = new TranslateAnimation(0, 10, 0, 0);
        //设置一个循环加速器，使用传入的次数就会出现摆动的效果。
        animation.setInterpolator(new CycleInterpolator(counts));
        animation.setDuration(500);
        animation.setRepeatCount(counts);
        animation.setRepeatMode(Animation.REVERSE);
        view.startAnimation(animation);
    }


    /**
     * 上下晃动动画
     *
     * @param counts 0.5秒钟晃动多少下
     * @return
     */
    public static void shakeAnimationVertical(int counts, View view) {
        Animation animation = new TranslateAnimation(0, 0, 0, 10);
        //设置一个循环加速器，使用传入的次数就会出现摆动的效果。
        animation.setInterpolator(new CycleInterpolator(counts));
        animation.setDuration(500);
        animation.setRepeatCount(counts);
        animation.setRepeatMode(Animation.REVERSE);
        view.startAnimation(animation);
    }


    /**
     * 0.1秒钟摇动3下
     * @param counts
     * @param view
     */
    public static void overshootAnimation(int counts, View view) {
        TranslateAnimation animation = new TranslateAnimation(0, -5, 0, 0);
        animation.setInterpolator(new OvershootInterpolator());
        animation.setDuration(100);
        animation.setRepeatCount(counts);
        animation.setRepeatMode(Animation.REVERSE);
        view.startAnimation(animation);
    }


    public static  void animationFlick(View view) {
        AlphaAnimation animation = new AlphaAnimation(1.0f,0.3f);
        animation.setInterpolator(new LinearInterpolator());
        animation.setRepeatCount(Animation.INFINITE);
        animation.setRepeatMode(Animation.REVERSE);
        animation.setDuration(500);
        view.startAnimation(animation);

    }

    public static  void clearAnimation(View view) {
        view.clearAnimation();
    }

    //透明层过度
    public static  void animationAlpha(View view) {
        AlphaAnimation animation = new AlphaAnimation(0.3f, 1.0f);
        animation.setDuration(1500);
        view.startAnimation(animation);
    }



    /**
     *
     *  // 将ImageView变成灰色
     * @param imageView
     */
    public static void toGrayImage(ImageView imageView) {

        ColorMatrix matrix = new ColorMatrix();
        matrix.setSaturation(0);//饱和度 0灰色 100过度彩色，50正常
        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
        imageView.setColorFilter(filter);
    }

    public static void toNormalImage(ImageView imageView) {
        imageView.setColorFilter(null);
    }

    public static void toHighImage(ImageView imageView) {

        ColorMatrix matrix = new ColorMatrix();
        matrix.setSaturation(100);//饱和度 0灰色 100过度彩色，50正常
        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
        imageView.setColorFilter(filter);
    }

    //安卓中像素px和dp的转换：
    public static int Dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density; //当前屏幕密度因子
        return (int) (dp * scale + 0.5f);
    }

    public static int Px2Dp(Context context, float px) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }
}
